package com.hongfeng.support.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hongfeng.support.annotation.Online;
import com.hongfeng.support.common.Page;
import com.hongfeng.support.controller.common.BaseController;
import com.hongfeng.support.item.ParmItem;
import com.hongfeng.support.item.tag.FieldOperator;
import com.hongfeng.support.model.BuyInfo;
import com.hongfeng.support.service.BuyInfoService;
import com.hongfeng.support.util.json.JsonUtils;

/**
 * 
 * <供求信息Controller>
 * 
 * @author llc
 * @date 创建时间：2012-12-5 下午02:26:49
 */
@Controller
@Online
public class BuyInfoController extends BaseController {

	@Resource
	private BuyInfoService buyInfoService;

	/**
	 * 如果方法参数不要求必须有的话，可以这样写： 例如：@RequestParam(value = "pageIndex", required =
	 * false)
	 * 
	 * @param request
	 * @param response
	 * @param pageIndex
	 * @param pageSize
	 * @param title
	 */
	@RequestMapping("list")
	public void list(HttpServletRequest request, HttpServletResponse response,
			String title, Page<BuyInfo> page) {
		log.info("供求信息：" + title);
		ParmItem item = new ParmItem("title", title, FieldOperator.LIKE);
		buyInfoService.findPageByQuery(page, BuyInfo.class, item);
		String json = JsonUtils.bean2Json(page);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("create")
	public void create(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("buyInfo") String buyInfoJson) {
		log.info("添加供求信息:" + (buyInfoJson != null ? buyInfoJson : null));
		if (buyInfoJson == null) {
			this.sendErrorResponse(response, "介绍不能为空。");
			return;
		}
		BuyInfo buyInfo = (BuyInfo) JsonUtils.json2Object(buyInfoJson,
				BuyInfo.class);
		buyInfoService.create(buyInfo);
	}

	@RequestMapping("update")
	public void update(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("buyInfo") String buyInfoJson) {
		log.info("修改供求信息:" + (buyInfoJson != null ? buyInfoJson : null));
		if (buyInfoJson == null) {
			this.sendErrorResponse(response, "供求信息不能为空。");
			return;
		}
		BuyInfo buyInfo = (BuyInfo) JsonUtils.json2Object(buyInfoJson,
				BuyInfo.class);
		BuyInfo qBuyInfo = buyInfoService.read(buyInfo.getId());
		buyInfo.setCreateTime(qBuyInfo.getCreateTime());
		buyInfo.setCreator(qBuyInfo.getCreator());
		buyInfoService.update(buyInfo);
	}

	@RequestMapping("read")
	public void read(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("id") Long id) {
		log.info("读取供求信息:" + id);
		BuyInfo buyInfo = buyInfoService.read(id);
		String json = JsonUtils.bean2Json(buyInfo);
		log.info("供求信息：" + json);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("ids") String ids) {
		log.info("删除供求信息:" + (ids != null ? ids.toString() : null));
		if (ids == null || "".equals(ids)) {
			this.sendErrorResponse(response, "请选择要删除的供求信息。");
			return;
		}
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			buyInfoService.delete(Long.parseLong(id));
		}
	}
}
